﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace MasApp.DAL
{
    public class Update
    {
        public Update()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public static int UpdateAppLevelValue(Model.applevel appl)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "UPDATE applevel "
                + " SET levelid = " + appl.Levelid
                + "  WHERE ("
                + " applevelid = "
                + appl.Applevelid
                + " )";
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        /// <summary>
        /// 更新任务状态为”在做的
        /// ”
        /// </summary>
        /// <param name="misid"></param>
        /// <returns></returns>
        public static int UpdateLevelMission(int misid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "UPDATE levelmission "
                + " SET state = '在做的' WHERE ("
                + " levelmissionid = "
                + misid
                + " )";
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        public static int UpdateWeightMission(int misid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "UPDATE weightmission "
                + " SET state = '在做的' WHERE ("
                + " weightmissionid = "
                + misid
                + " )";
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        /// <summary>
        /// 通过we更新权重值，返回影响行数
        /// </summary>
        /// <param name="we"></param>
        /// <returns></returns>
        public static int UpdateWeight(Model.weight we)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "UPDATE weight "
                + " SET weightvalue = " 
                + we.Weightvalue
                +" WHERE ("
                + "weightmissionid = "
                + we.Weightmissionid
                + " AND elementtype = '"
                + we.Elementtype
                + "' AND elementid = "
                + we.Elementid
                + ")";
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        /// <summary>
        /// 更新密码，返回影响行数
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static int UpdatePassword(string username,string password)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "update login set password = '" + password + "' where username = '" + username + "'";
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        /// <summary>
        /// 提交评估方法
        /// </summary>
        /// <param name="methodid"></param>
        /// <returns></returns>
        public static bool SubmitMethod(int methodid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "update method set available = 'true' where methodid = " + methodid ;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i > 0 ? true : false;
        }
        /// <summary>
        /// 提交模型，返回成功与否状态
        /// </summary>
        /// <param name="modelid"></param>
        /// <returns></returns>
        public static bool SubmitModel(int modelid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SetModelAvailable";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@modelid", SqlDbType.Int).Value = modelid;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i > 0 ? true : false;
        }
        /// <summary>
        /// 启动项目，返回影响行数
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int StartProject(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "update project set state = '在评的' where projectid = " + projectid;
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        /// <summary>
        /// 更新项目，返回影响行数
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public static int UpdateProject(Model.project pro)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "update project set projectname = '"
                + pro.Projectname + "', modelid = "
                + pro.Modelid + ", methodid = "
                + pro.Methodid + ", starttime = '"
                + pro.Starttime + "', endtime = '"
                + pro.Endtime + "', description = '"
                + pro.Description + "' where projectid = " + pro.Projectid;
            cmd.CommandText = str;
            int i = 0;
            try
            {

                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
        //通用更新方法，返回影响行数
        public static int UpdateINT(string commandtext)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = commandtext;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
    }
}